package com.txm.service;

import com.txm.qqcommon.Message;
import com.txm.qqcommon.MessageType;
import com.txm.qqcommon.User;
import com.txm.qqview.QQView;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * 当用户登录成功后，启动一个接受服务器消息的线程
 */
public class ClientConnectServerThread extends Thread {
    private Socket socket;

    public boolean islive = true;
    public QQView qqView;

    public User user;

    public ClientConnectServerThread(Socket socket, QQView qqView) {
        this.socket = socket;
        this.qqView = qqView;
        this.qqView.ccst = this;
    }

    @Override
    public void run() {
        try {
            while (islive) {
//                System.out.println(ManageServerThreads.getMcts());
//                System.out.println("等待消息");
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                Message msg = (Message) objectInputStream.readObject();
//                System.out.println("ClientConnectServerThread:" + msg);
                switch (msg.getMsgType()) {
                    case MessageType.MESSAGE_GET_ONLINE_USER:
                        qqView.viewOnlineUser(msg);
                        break;
                    case MessageType.MESSAGE_SENDER_FILE_USER:
                        qqView.transferFiles.start();
                        break;
                    case MessageType.MESSAGE_ONLINE_USER:
                    case MessageType.MESSAGE_NOT_ONLINE_USER:
                        qqView.personChatDetail(msg, socket);
                        break;
                    case MessageType.MESSAGE_CHAT_USER:
                    case MessageType.MESSAGE_NOT_USER:
                    case MessageType.MESSAGE_CHAT_GROUP:
                    case MessageType.MESSAGE_CHAT_GROUP_EXIT:
                    case MessageType.MESSAGE:
                        qqView.personChatBack(msg, socket);
                        break;
                    case MessageType.MESSAGE_RECEIVE_FILE:
                        ReceiveFile.handleReceiveFile(msg);
                        break;
                    case MessageType.MESSAGE_SENDER_MSG_GROUP:
                        qqView.groupChatDetail(msg,socket);
                        break;
                }
                if (msg.getMsgType().equals(MessageType.MESSAGE_SYSTEM_EXIT)) {
                    islive = false;
                    System.out.println(msg.getContent());
                    break;
                }
            }

            socket.close();
            System.out.println(this.getName() + "线程结束。");
//          退出系统 0：正常退出，非零是异常退出
            System.exit(0);

        } catch (Exception e) {
            System.out.println("CCST Exception：" + e);
//            e.printStackTrace();
            if (e.toString().equals("java.net.SocketException: Connection reset")) {
                islive = false;
                try {
                    socket.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                System.out.println("服务器连接中断，退出当前连接。");
            }
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public boolean isIslive() {
        return islive;
    }

    public void setIslive(boolean islive) {
        this.islive = islive;
    }
}
